/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.mybeans.dao.DAOException;
import org.mybeans.forms.FormBeanFactory;


import scheduler.formbean.DeleteForm;
import scheduler.model.Model;
import scheduler.model.ScheduleDao;



public class RemoveAction extends Action {
	private FormBeanFactory<DeleteForm> formBeanFactory = FormBeanFactory.getInstance(DeleteForm.class,"<>\"");

	private ScheduleDao scheduleDao;


    public RemoveAction(Model model) {
    	scheduleDao = model.getScheduleDao();
	}

    public String getName() { return "delete.do"; }

    public String perform(HttpServletRequest request) {
    	DeleteForm form = formBeanFactory.create(request);
    	
        // Set up the request attributes (the errors list and the form bean so
        // we can just return to the jsp with the form if the request isn't correct)
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
		

		try {
			int id = form.getId();
			scheduleDao.delete(id);

    
	        return "schedule.do";
		} catch (DAOException e) {
    		errors.add(e.getMessage());
    		return "error.jsp";
    	}
    }
}
